package org.apache.lucene.util;

public class IntArrayList {
	private int[] array;
	private int size;
	private double growthFactor;

	public IntArrayList(int initialSize, double growthFactor) {
		array = new int[initialSize];
		this.growthFactor = growthFactor;
		size = 0;
	}
  
	public IntArrayList(int[] array) {
		this.array = array;
		this.size = array.length;
		this.growthFactor = 1.25;
	}
	
	public void setGrowthFactor(double growthFactor) {
		this.growthFactor = growthFactor;
	}
	
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		for (int x=0; x < size; x++) {
			buffer.append(array[x]);
			if (x < size-1)
				buffer.append(',');
		}
		return buffer.toString(); 
	}
	
	public int size() {
		return size;
	}
	
	public void add(int value) {
		ensureCapacity(size+1);
		array[size] = value;
    size++;
	}
	
	public int ensureCapacity(int capacity) {
		if (capacity > array.length) {
			int newCapacity = (int)(array.length * growthFactor);
			int[] newArray = new int[newCapacity];
			System.arraycopy(array, 0, newArray, 0, size);
			array = newArray;
			capacity = newCapacity;
		}
		return capacity;
	}

	public void clear() {
		size = 0;
	}

	public int[] toIntArray() {
		int[] intArray = new int[size];
		System.arraycopy(array, 0, intArray, 0, size);
		return intArray;
	}
}
